﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Caliburn.Micro;
using SS.Common.Utilities;
using SS.WPF.Framework.Localization;
using SS.WPF.Framework.Restriction;
using SS.WPF.Framework.Selection;
using SS.WPF.Framework.ViewModel.Container;
using SS.WPF.Framework.ViewModel.Navigation;
using TestFramework.Common;

namespace TestFramework.ViewModels
{
    using System.ComponentModel.Composition;

    [Export(typeof(IShell))]
    public class ShellViewModel : AllActiveViewModel<IScreen>, IShell
    {
        private INavigation _navigationViewModel;
        [Import(typeof(INavigation))]
        public INavigation NavigationViewModel
        {
            get
            {
                return this._navigationViewModel;
            }
            set
            {
                this._navigationViewModel = value;
                this.NotifyOfPropertyChange(() => this.NavigationViewModel);
            }
        }

        private EditingViewModel _editingViewModel;
        [Import]
        public EditingViewModel EditingViewModel
        {
            get
            {
                return this._editingViewModel;
            }
            set
            {
                this._editingViewModel = value;
                this.NotifyOfPropertyChange(() => this.EditingViewModel);
            }
        }

        [Import]
        public ISelectionService SelectionService { get; set; }

        protected override void OnInitialize()
        {
            this.Items.Add(this._navigationViewModel as IScreen);
            this.Items.Add(this._editingViewModel);
            this.ActivateItem(this._editingViewModel);

            base.OnInitialize();
        }

        #region Event handlers

        public void CollapseNavigation()
        {
            foreach (var catalog in this.NavigationViewModel.NavigationCatalogs)
            {
                catalog.IsExpanded = false;
            }
        }

        public void ExpandNavigation()
        {
            foreach (var catalog in this.NavigationViewModel.NavigationCatalogs)
            {
                catalog.IsExpanded = true;
            }
        }

        /// <summary>
        /// Notethat: must be execute this function by IResult with LongRun mode
        /// If not the event raised later will be lost
        /// </summary>
        /// <returns></returns>
        public IEnumerable<IResult> LoadData()
        {
            yield return this.BuildLongRun().Do(() => this.SelectionService.SetSelection(this, null));
        }

        #endregion
    }
}